/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-01 09:02:02
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:39:23
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { StoreFunctionsServiceProxy, StorePutServiceProxy, StorePutModelContentDto, CommonServiceProxy, PartsDeliveryServiceProxy, OperationHistoryServiceProxy, OperationLogDto, LabelInformation, MaterialDetails } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MatingExWarehouseComponent } from './mating-ex-warehouse/mating-ex-warehouse.component';
import { AddExComponent } from './add-ex/add-ex.component';
import { ScrapExComponent } from './scrap-ex/scrap-ex.component';
import { RepairExComponent } from './repair-ex/repair-ex.component';
import { RejectExComponent } from './reject-ex/reject-ex.component';
import { ReturnWarehouseComponent } from './return-warehouse/return-warehouse.component';
import { TreeNode } from 'primeng/api';

@Component({
    selector: 'app-ex-warehouse',
    templateUrl: './ex-warehouse.component.html',
    styleUrls: ['./ex-warehouse.component.less']
})
export class ExWarehouseComponent extends AppComponentBase {
    @ViewChild('MatingExWarehouseComponent') MatingExWarehouseComponent: MatingExWarehouseComponent;
    @ViewChild('AddExComponent') AddExComponent: AddExComponent;
    @ViewChild('ScrapExComponent') ScrapExComponent: ScrapExComponent;
    @ViewChild('RepairExComponent') RepairExComponent: RepairExComponent;
    @ViewChild('RejectExComponent') RejectExComponent: RejectExComponent;
    @ViewChild('ReturnWarehouseComponent') ReturnWarehouseComponent: ReturnWarehouseComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    stroeName = undefined;//入库条件
    stroeNameOptions = [];
    storePutNo = undefined;
    storePutNoOptions = [];
    sstroeName = undefined;//入库查询条件
    productDrawingNo = undefined;
    qualifiedNumber = undefined;
    taskNumber = undefined;
    taskNumberOptions = [];
    model = undefined;
    modelOptions = [];
    sendTime = undefined;
    sendTimeOptions = undefined;
    rowdata: any;
    title = '';
    deleteRow: any;
    warehouseData: any;
    confirmFlag = false;
    actived = false;
    form = new StorePutModelContentDto();
    binLocationOptions = [];
    llVisible: boolean = false;
    showDetailVisible: boolean = false;
    logList: OperationLogDto[];
    shortageVisible: boolean = false;
    treeTableSelect: any = []
    showDetailListSelect: any = []
    lookforMatchingOutputList: LabelInformation = new LabelInformation();
    checktype: any;
    showDetailList: MaterialDetails[];
    lookDetailType: any;
    detailInfo: any;
    saveFlag = true;
    assemblyTaskNumber = undefined;
    releaseOrder = undefined;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
        private _partsDeliveryServiceProxy: PartsDeliveryServiceProxy,
        private _operationHistoryServiceProxy: OperationHistoryServiceProxy,
    ) {
        super(injector);
    }
    openUrl() {
        window.open('http://10.20.253.4:8057/#/app/material/index');
    }
    reset() {
        this.productDrawingNo = undefined;
        this.assemblyTaskNumber = undefined;
        this.qualifiedNumber = undefined;
        this.releaseOrder = undefined;
        this.model = undefined
        this.sendTime = undefined
        this.sstroeName = undefined
        this.form = new StorePutModelContentDto();
    }
    //履历
    llVisibleEv(id) {
        this.llVisible = true
        this._operationHistoryServiceProxy.getOperationLogList(
            id
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.logList = result
            });
    }
    // 展开
    expandAll(rs) {
        for (const node of rs) {
            this.TreeExpandRecursive(node, true);
        }
    }
    private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.TreeExpandRecursive(childNode, isExpand);
            });
        }
    }
    // 配套出库 查看的查看
    lookDetail(item, type, operation) {
        this.lookDetailType = type
        this.detailInfo = item
        this._partsDeliveryServiceProxy.getMaterialDetails(
            this.warehouseData.id,
            item.id,
            item.loadDetailsId,
            operation
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.showDetailVisible = true
                this.showDetailList = result.materialDetails
            });
    }
    // 查看 type 1查看 2修改
    lookResume(item, type) {
        this.checktype = type
        this.warehouseData = item
        // item.retrievalStateDesc 配套 新增 报废 返修 驳回 
        switch (item.retrievalStateDesc) {
            case '配套':
                if (type == 1) {//查看
                    this.shortageVisible = true
                    this.warehouseData = item

                    this.lookforMatchingOutputEv(item.id)
                } else if (type == 2) {//修改
                    this.warehouseData = item
                    setTimeout(() => {
                        this.ReturnWarehouseComponent.show();
                    }, 10);
                } else if (type == 3) {//退库
                    this.warehouseData = item
                    setTimeout(() => {
                        this.ReturnWarehouseComponent.show();
                    }, 10);
                }
                break;
            case '新增':
                setTimeout(() => {
                    this.AddExComponent.show();
                }, 10);
                //  this.
                this.warehouseData = item

                break;
            case '报废':
                setTimeout(() => {
                    this.ScrapExComponent.show();
                }, 10);
                break;
            case '返修':
                setTimeout(() => {
                    this.RepairExComponent.show();
                }, 10);
                break;
            case '驳回':
                setTimeout(() => {
                    this.RejectExComponent.show();
                }, 10);
                break;
            default:
                break;
        }
    }
    // 确认
    sureResume(item) {
        if (this.saveFlag) {
            this.saveFlag = false;
            this._messageService.confirm(
                ' 确认产品出库！！！！',
                (isConfirmed) => {
                    if (isConfirmed) {
                        this._partsDeliveryServiceProxy.confirmDelivery(
                            item.id
                        ).pipe(finalize(() => { }))
                            .subscribe(result => {
                                this.saveFlag = true;
                                if (result.code == 0) {
                                    this._messageService.success('操作成功');
                                    this.getTableList();
                                } else {
                                    this._messageService.error(result.message);
                                }
                            });
                    }
                }
            );
        }
    }

    // 配套修改 /ProductionDesignAppService/
    modifMatchingOutputEv(id) {
        this._partsDeliveryServiceProxy.modifMatchingOutput(
            id
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.lookforMatchingOutputList = result
                this.expandAll(this.lookforMatchingOutputList.boundOutputs);
                this.treeTableSelect = this.lookforMatchingOutputList.checkOutputs
            });
    }
    // 配套查看 /ProductionDesignAppService/
    lookforMatchingOutputEv(id) {
        this._partsDeliveryServiceProxy.lookforMatchingOutput(
            id
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.lookforMatchingOutputList = result
                this.expandAll(this.lookforMatchingOutputList.boundOutputs);
                this.treeTableSelect = this.lookforMatchingOutputList.checkOutputs
            });
    }
    // 弹窗隐藏
    displayclose() { }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        let model = undefined;
        if (this.model != undefined) {
            for (const item of this.modelOptions) {
                if (item.value == this.model) {
                    model = item.label;
                }
            }
        }
        this._partsDeliveryServiceProxy.getDeliverys(
            this.sstroeName,
            this.productDrawingNo,
            this.assemblyTaskNumber,
            this.qualifiedNumber,
            this.releaseOrder,
            this.model,
            this.sendTime,
            undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    rejectEx() {
        this.checktype = undefined;
        setTimeout(() => {
            this.RejectExComponent.show();
        }, 10);
    }
    repairEx() {
        this.checktype = undefined;
        setTimeout(() => {
            this.RepairExComponent.show();
        }, 10);
    }
    returnEx() {
        this.checktype = undefined;
        setTimeout(() => {
            this.ReturnWarehouseComponent.show();
        }, 10);
    }
    addEx() {
        this.checktype = undefined;
        setTimeout(() => {
            this.AddExComponent.show();
        }, 10);
    }
    scrapEx() {
        this.checktype = undefined;
        setTimeout(() => {
            this.ScrapExComponent.show();
        }, 10);
    }
    matingExWhs() {
        this.checktype = undefined;
        //配套出库
        setTimeout(() => {
            this.MatingExWarehouseComponent.show();
        }, 10);
    }
    ngOnInit() {
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOptions = [];
                for (const item of result) {
                    let obj = { label: undefined, value: undefined };
                    obj.label = item.label;
                    obj.value = item.value;
                    this.modelOptions.push(obj);
                }
                // this.modelOptions = result;
            })
        //仓库名称
        this._storeFunctionsServiceProxy.getStoreDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.stroeNameOptions = [];
                this.stroeNameOptions = result;
            });
    }
    onChange(event) {
         //发次
        this._commonServiceProxy.getLotNoList(
             this.model
            )
                .pipe(finalize(() => { })).subscribe(result => {
                    // this.sendTimeOptions = result;
                    this.sendTimeOptions = [];
                    for (const item of result) {
                        let obj = { label: undefined, value: undefined };
                        obj.label = item.label;
                        obj.value = item.label;
                        this.sendTimeOptions.push(obj);
                    }
                })
    }
}
